﻿
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.DatabaseServices;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using XS.Tunnel.Cad.SteelBracing.Infrastructure.Extensions;


namespace XS.Tunnel.Cad.SteelBracing.Infrastructure
{
    public abstract class CadBase
    {
        public Document Doc { 
            get {
                return Application.DocumentManager.MdiActiveDocument;
            }
        }
     


        protected void Drawing(Action<BlockTableRecord, Transaction> action)
        {
            //Document doc = Application.DocumentManager.MdiActiveDocument;
            Database db = Doc.Database;
            // 开启事务
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                //打开块表
                var bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                //打开块表记录
                var btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                action(btr, trans);
                //事务提交
                trans.Commit();
            }
        }

        public void Alert(string msg)
        {
            Application.ShowAlertDialog(msg); 
        }


        protected void Drawing(List<Entity> entities)
        {
            Drawing((btr, trans) =>
            {
                foreach (var item in entities)
                {
                    btr.AppendEntity(item);
                    trans.AddNewlyCreatedDBObject(item, true);
                }
            });
        }
        protected void Drawing(List<Curve> entities,string color="")
        {
            Drawing((btr, trans) =>
            {
                foreach (var item in entities)
                {
                    if (!string.IsNullOrEmpty(color))
                    {
                        item.Color = color.ToColor();
                    }
                    btr.AppendEntity(item);
                    trans.AddNewlyCreatedDBObject(item, true);
                }
            });
        }

        protected void Drawing(params Entity[] entities)
        {
            Drawing(entities.ToList());
        }

        protected void Drawing(params ISysPoint[] entities)
        {
            Drawing(entities.ToEntitys());
        }









    }
}
